﻿Imports Framework.DataServices
Imports Ita.ProElicere.Dao.Ita.ProElicere.Dao
Imports System.Data

Namespace Ita.ProElicere

    Public Class UserDao : Inherits CommonDatabase : Implements IUserDao

        Public Function Insert(currentUser As String, entity As Model.Ita.ProElicere.Model.User) As Integer Implements IBasicCrudDao(Of Model.Ita.ProElicere.Model.User).Insert


            Insert = 0
        End Function
        Public Function Delete(currentUser As String, entity As Model.Ita.ProElicere.Model.User) As Integer Implements IBasicCrudDao(Of Model.Ita.ProElicere.Model.User).Delete

            Delete = 0
        End Function
        Public Function Update(currentUser As String, entity As Model.Ita.ProElicere.Model.User) As Integer Implements IBasicCrudDao(Of Model.Ita.ProElicere.Model.User).Update

            Update = 0
        End Function
        Public Function Load(currentUser As String, entity As Model.Ita.ProElicere.Model.User) As Model.Ita.ProElicere.Model.User Implements IBasicCrudDao(Of Model.Ita.ProElicere.Model.User).Load

            Dim table As DataTable = MyBase.ExecuteFill(CommandType.StoredProcedure, "sp_SELECT_User", False, currentUser,
                MyBase.Parameters.Create("@code", DbType.Int32, entity.UserID),
                MyBase.Parameters.Create("@ReturnValue", DbType.Int32, ParameterDirection.ReturnValue, Nothing)
                )

            Dim rowsFound As Integer = Convert.ToInt32(MyBase.ExecutionStatus.Command.Parameters("@ReturnValue").Value)

            If (table.Rows.Count > 0) Then
                Load = ConvertRowToItem(table.Rows(0))
                Exit Function
            End If

            Load = Nothing

        End Function
        Public Function SelectAll(currentUser As String) As List(Of Model.Ita.ProElicere.Model.User) Implements IBasicCrudDao(Of Model.Ita.ProElicere.Model.User).SelectAll


            Dim table As DataTable = MyBase.ExecuteFill(CommandType.StoredProcedure, "sp_SELECT_User_All", False, currentUser,
                MyBase.Parameters.Create("@ReturnValue", DbType.Int32, ParameterDirection.ReturnValue, Nothing)
                )

            Dim rowsFound As Integer = Convert.ToInt32(MyBase.ExecutionStatus.Command.Parameters("@ReturnValue").Value)

            

            SelectAll = ConvertTableToList(table)

        End Function


        Public Function ConvertRowToItem(row As DataRow) As Model.Ita.ProElicere.Model.User

            Dim entity As New Model.Ita.ProElicere.Model.User()


            If (row.Table.Columns.Contains("code") And Not (Convert.IsDBNull(row("code")))) Then
                entity.UserID = Convert.ToInt32(row("code"))
            End If

            If (row.Table.Columns.Contains("name") And Not (Convert.IsDBNull(row("name")))) Then
                entity.Name = Convert.ToString(row("name"))
            End If

            ConvertRowToItem = entity

        End Function
        Public Function ConvertTableToList(table As DataTable) As List(Of Model.Ita.ProElicere.Model.User)

            Dim res As New List(Of Model.Ita.ProElicere.Model.User)

            For Each row In table.Rows
                res.Add(ConvertRowToItem(row))
            Next

            ConvertTableToList = res

        End Function


    End Class

End Namespace

